import 'package:leader_help/entrance/route/routes.dart';
import 'package:get/get.dart';
import 'model/welcome.dart';

import 'index.dart';

class WelcomeController extends GetxController {
  WelcomeController();

  final state = WelcomeState();

  void _observer() {
    state.pageCtrl.addListener(() {
      var _maxOffset = ((state.list?.length ?? 1) - 1) * Get.width + 50;
      if (state.pageCtrl.offset > _maxOffset) {
        toMain();
      }
    });
  }

  void toMain() {
    Get.offAllNamed(RouterPath.main);
  }

  void _getData() {
    state.list = [
      WelcomeModel()
        ..url =
            "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fcmsdownload.sangfor.com.cn%2F-%2Fmedia%2F56AED1E2889F41B8B785CB79AE88849F.jpg%3Fh%3D900%26la%3Dzh-CN%26w%3D1600%26rev%3D2948fffacfe0465387ee6e26f1feb8f3%26hash%3D12413713BA75878A616F43D4BF303B53&refer=http%3A%2F%2Fcmsdownload.sangfor.com.cn&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1648361648&t=b5245650a2f6636fa290604f53bd6d4c",
      WelcomeModel()
        ..url =
            "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fwww.cnet99.com%2Fai%2Fimages%2FTB1uR6XbAP2gK0jSZPxXXacQpXa-820-565.png&refer=http%3A%2F%2Fwww.cnet99.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1648361728&t=18806d71c730af579bc3dbeeb0b0a062",
      WelcomeModel()
        ..url =
            "https://gimg2.baidu.com/image_search/src=http%3A%2F%2F5b0988e595225.cdn.sohucs.com%2Fimages%2F20181023%2F51e0f6145ab64c94927fceeef5a41195.jpeg&refer=http%3A%2F%2F5b0988e595225.cdn.sohucs.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1648362155&t=8176a6147ac562c7b92756955ea4fc4c",
    ];
    update();
  }

  /// 在 widget 内存中分配后立即调用。
  @override
  void onInit() {
    super.onInit();
    _observer();
    _getData();
  }

  /// 在 onInit() 之后调用 1 帧。这是进入的理想场所
  @override
  void onReady() {
    super.onReady();
  }

  /// 在 [onDelete] 方法之前调用。
  @override
  void onClose() {
    super.onClose();
    state.pageCtrl.dispose();
  }

  /// dispose 释放内存
  @override
  void dispose() {
    super.dispose();
  }
}
